# DjangoDE, an integrated development environment for Django
# Copyright (C) 2010-2011 Andrew Wilkinson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import sys
import time
import unittest

from djangode import global_objects
from djangode.gui import MainWindow
from djangode.utils.qt import Qt, QtCore, QtGui

class TestFunctionTooltip(unittest.TestCase):
    def setUp(self):
        if hasattr(sys, "last_type"):
            del sys.last_type

        global_objects.main_window = MainWindow()
        global_objects.main_window.show()

    def tearDown(self):
        global_objects.main_window.close()
        global_objects.main_window = None

    def test_tooltip(self):
        """Test that when you type 'max(' you get a tooltip."""
        global_objects.main_window.actions["new_file"].trigger()
        editor = global_objects.main_window.editor.widget(0)
        editor.setFocus(QtCore.Qt.OtherFocusReason)

        for char in "max(":
            self.send_event(editor, QtGui.QKeyEvent(QtCore.QEvent.KeyPress, ord(char), QtCore.Qt.NoModifier, char))

        self.assert_(QtGui.QToolTip.isVisible())

    def test_tooltip_close_on_close_bracket(self):
        """Test that when you type 'max(' you get a tooltip, and then when you close the bracket it closes."""
        global_objects.main_window.actions["new_file"].trigger()
        editor = global_objects.main_window.editor.widget(0)
        editor.setFocus(QtCore.Qt.OtherFocusReason)

        for char in "max(":
            self.send_event(editor, QtGui.QKeyEvent(QtCore.QEvent.KeyPress, ord(char), QtCore.Qt.NoModifier, char))

        self.assert_(QtGui.QToolTip.isVisible())

        for char in "a, b":
            self.send_event(editor, QtGui.QKeyEvent(QtCore.QEvent.KeyPress, ord(char), QtCore.Qt.NoModifier, char))

        self.assert_(QtGui.QToolTip.isVisible())

        self.send_event(editor, QtGui.QKeyEvent(QtCore.QEvent.KeyPress, ord(char), QtCore.Qt.NoModifier, ")"))

        global_objects.app.processEvents()
        time.sleep(2) # Tool tips fade out
        global_objects.app.processEvents()
        
        self.assert_(not QtGui.QToolTip.isVisible())
        
    def send_event(self, editor, event):
        global_objects.app.sendEvent(editor.completer.popup() if editor.completer.popup().isVisible() else editor, event)
        global_objects.app.processEvents()
